﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.datatype;
using umbraco.DataLayer;
using System.IO;
using System.Web;

namespace Decode.Umb.Pixlr
{
	public class pixlrPrevalueEditor : System.Web.UI.WebControls.PlaceHolder, umbraco.interfaces.IDataPrevalue
	{
		#region IDataPrevalue Members

		private umbraco.cms.businesslogic.datatype.BaseDataType _datatype;
		private RadioButtonList _rblPixlrEditor;

		public pixlrPrevalueEditor(umbraco.cms.businesslogic.datatype.BaseDataType DataType)
		{
			_datatype = DataType;
			setupChildControls();
		}


		private void setupChildControls()
		{
			// editor type
			_rblPixlrEditor = new RadioButtonList();
			_rblPixlrEditor.ID = "rblPixlrEditor";

			ListItem li1 = new ListItem();
			li1.Text = "Pixlr Editor - Advanced image editing.";
			li1.Value = "editor";
			ListItem li2 = new ListItem();
			li2.Text = "Pixlr Express - Simple image editing.";
			li2.Value = "express";

			_rblPixlrEditor.Items.Add(li1);
			_rblPixlrEditor.Items.Add(li2);
			Controls.Add(_rblPixlrEditor);
		}


		public Control Editor
		{
			get
			{
				return this;
			}
		}


		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			if (!Page.IsPostBack)
			{
				_rblPixlrEditor.SelectedValue = GetEditorTypeSetting;
			}
		}


		public void Save()
		{
			_datatype.DBType = (umbraco.cms.businesslogic.datatype.DBTypes)Enum.Parse(typeof(umbraco.cms.businesslogic.datatype.DBTypes), DBTypes.Ntext.ToString(), true);

			string editorType = _rblPixlrEditor.SelectedValue;

			SqlHelper.ExecuteNonQuery("delete from cmsDataTypePreValues where datatypenodeid = @dtdefid",
								SqlHelper.CreateParameter("@dtdefid", _datatype.DataTypeDefinitionId));

			SqlHelper.ExecuteNonQuery("insert into cmsDataTypePreValues (datatypenodeid,[value],sortorder,alias) values (@dtdefid,@value,0,'editorType')",
								SqlHelper.CreateParameter("@dtdefid", _datatype.DataTypeDefinitionId), SqlHelper.CreateParameter("@value", editorType));
		}

		protected override void Render(HtmlTextWriter w)
		{
			// verify that the crossdomain file is in place
			// a common problem
			if (File.Exists(HttpContext.Current.Server.MapPath("../../../crossdomain.xml")) == false)
			{
				w.WriteLine("<div class=\"error\">");
				w.WriteLine("<p>It looks like you are missing the crossdomain.xml file from the root of your website?</p>");
				w.WriteLine("<p>Pixlr will most likely not work without it. You can get the file from the <a href=\"http://our.umbraco.org/projects/backoffice-extensions/pixlr/\" target=\"_blank\">Pixlr package</a>.</p>");
				w.WriteLine("</div>");
			}


			w.WriteLine("<div class=\"propertyItem\">");
			w.WriteLine("<div class=\"propertyItemheader\">Choose editor</div>");
			_rblPixlrEditor.RenderControl(w);
			w.WriteLine("<br style=\"clear:both\"/>");
			w.WriteLine("</div>");


			// custom link text notice
			w.WriteLine("<div class=\"propertyItem\">");
			w.WriteLine("<div class=\"propertyItemheader\"></div>");
			w.WriteLine("You can change the default editor link text \"Edit image\" by adding &quot;pixlrLink&quot; to your language file in the umbraco/config/lang folder.");
			w.WriteLine("<br style=\"clear:both\"/>");
			w.WriteLine("</div>");
		}


		public string GetEditorTypeSetting
		{
			get
			{
				object conf = SqlHelper.ExecuteScalar<object>("select value from cmsDataTypePreValues where datatypenodeid = @datatypenodeid and alias = 'editorType'",
																											SqlHelper.CreateParameter("@datatypenodeid", _datatype.DataTypeDefinitionId));

				if (conf != null)
					return conf.ToString();
				else
					return "editor";
			}
		}

		#endregion


		public static ISqlHelper SqlHelper
		{
			get { return Application.SqlHelper; }
		}
	}
}